package com.qf.listen;

import com.qf.dao.OrderRepository;
import com.qf.pojo.vo.QfOrder;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class PayListen {

    @Autowired
    OrderRepository orderRepository;

    private static final Logger logger = LoggerFactory.getLogger(PayListen.class);
    @RabbitListener(queues = "pay-queue")
    public void PayQueue(String orderid, Channel channel, Message message){
        try {
            //通过订单id获取该笔订单的详细信息
            Optional<QfOrder> byId = orderRepository.findById(orderid);
            if (!byId.isPresent()){
                throw new Exception("没有该订单");
            }
            QfOrder qfOrder = byId.get();
            //说明用户3分钟内一直未支付
            if (qfOrder.getOrderStatus()==3){
                //将支付状态更改未 取消支付状态
                qfOrder.setOrderStatus(4);
                orderRepository.saveAndFlush(qfOrder);
                //将该条消息进行确认
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }
        }catch (Exception e){

            logger.error("订单id为："+orderid+"信息出现异常！"+e.getMessage());
        }
    }
}
